﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.BLL
{
    public class UserQueryBuilder
    {
        public static void PraseQuery(string query_key)
        {
            string query_select1 = HttpContext.Current.Request.Form["query_select1"];
            string query_operator1 = HttpContext.Current.Request.Form["query_operator1"];
            string query_result1 = HttpContext.Current.Request.Form["query_result1"];

            string query_select2 = HttpContext.Current.Request.Form["query_select2"];
            string query_operator2 = HttpContext.Current.Request.Form["query_operator2"];
            string query_result2 = HttpContext.Current.Request.Form["query_result2"];
            string query_andor = HttpContext.Current.Request.Form["query_andor"];
            string query1 = "";
            string query2 = "";
            string query = "";
            //验证是否是从查询窗口请求的
            if (query_operator1.IsNull()) return;
            if (query_select1.IsNotNull() && query_result1.IsNotNull())
            {
                query1 = BuildQuery(query_select1, query_operator1, query_result1);
            }
            if (query_select2.IsNotNull() && query_result2.IsNotNull())
            {
                query2 = BuildQuery(query_select2, query_operator2, query_result2);
            }

            if (query1.IsNull() && query2.IsNotNull())
            {
                query = query2;
            }
            if (query2.IsNull() && query1.IsNotNull())
            {
                query = query1;
            }
            string op = query_andor=="operator_and"?"AND":"OR";
            if (query2.IsNotNull() && query1.IsNotNull())
            {
                query = query1 + " "+op +" " + query2;
            }
            HttpContext.Current.Session[query_key] = query;

        }
        static string GetQueryKey()
        {
            string key = HttpContext.Current.Request.Url + "_UserQueryString";
            key = key.Replace("PartialView", "");
            return key;
        }

        public static void ClearQuery(string query_key)
        {
            HttpContext.Current.Session.Remove(query_key);
        }
        public static string GetQueryString(string query_key)
        {
            return HttpContext.Current.Session[query_key].ToSafeString();
        }

        public static IQueryable AppendQuery(IQueryable query,string query_key)
        {
            UserQueryBuilder.PraseQuery(query_key);
            string filter = GetQueryString(query_key);
            if (filter.IsNotNull())
                return query.ApplyFilter(filter);
            return query;
        }

        static string BuildQuery(string field, string op, string value)
        {
            string key = field.Split(';')[0];
            value = value.Trim();

            TypeofQueryColumn data_type = (TypeofQueryColumn)Enum.Parse(typeof(TypeofQueryColumn), field.Split(';')[1]);
            if (data_type == TypeofQueryColumn.Text||data_type== TypeofQueryColumn.DropDownList)
            {
                value = "'" + value + "'";
            }
            if (data_type == TypeofQueryColumn.Date)
            {
                value = "#" + value + "#";
            }

            if (op == "contains")
            {
                return "Contains([{0}], {1})".FormatWith(key, value);
            }
            if (op == "equals")
            {
                return "[{0}] = {1}".FormatWith(key, value);
            }
            if (op == "not_equals")
            {
                return "[{0}] <> {1}".FormatWith(key, value);
            }
            if (op == "greater")
            {
                return "[{0}] > {1}".FormatWith(key, value);
            }
            if (op == "less")
            {
                return "[{0}] < {1}".FormatWith(key, value);
            }

            //if (op == "contains")
            //{
            //    return "Contains([{0}], '{1}')".FormatWith(key,value);
            //}
            //if (op == "equals")
            //{
            //    return "[{0}] = '{1}'".FormatWith(key, value); 
            //}
            //if (op == "not_equals")
            //{
            //    return "[{0}] <> '{1}'".FormatWith(key, value); 
            //}
            //if (op == "greater")
            //{
            //    return "[{0}] > '{1}'".FormatWith(key, value);
            //}
            //if (op == "less")
            //{
            //    return "[{0}] < '{1}'".FormatWith(key, value);
            //}


            return null;
        }
    }
}